package com.sgb.kjwl.view.ui.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.swgk.core.BaseAppApplication;
import com.swgk.core.BaseAppHelper;
import com.swgk.core.base.di.AppComponent;

/***
 * 2021.03.31
 * 使用viewpage2创建Fragment时，懒加载基类
 * @author hq
 */
public abstract class LazyFragment2 extends Fragment implements View.OnClickListener {

    private static final int MIN_CLICK_DELAY_TIME = 2000;
    private long lastClickTime;
    protected Context context;
    /***
     * 是否第一次加载
     */
    private boolean isOnResumed = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context = getActivity();
        setupFragmentComponent(BaseAppHelper.get().getAppComponent());
    }

    protected abstract void setupFragmentComponent(AppComponent appComponent);

    @Override
    public void onClick(View v) {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            lastClickTime = curClickTime;
            onMultiClick(v);
        }
    }

    protected abstract void onMultiClick(View v);

    /***
     * 实现懒加载关键代码
     * Fragment在切换时，其生命周期方法的执行情况我们发现不管Fragment是否会被预先创建，
     * 只有可见时才会执行到onResume()方法，我们正好可以利用这一规律来实现懒加载
     */
    @Override
    public void onResume() {
        super.onResume();
        if (!isOnResumed) {
            initView();
            loadData();
            initEvent();
            isOnResumed = true;
        }
    }

    /**
     * 初始化视图
     */
    protected void initView() {
    }

    /**
     * 初始化数据(请求接口)
     */
    protected void loadData() {
    }

    /**
     * 刷新数据
     */
    protected void refreshData() {
    }

    /**
     * 初始化事件
     */
    protected void initEvent() {
    }
}
